/**
 * 
 */
package cn.gov.msa.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import com.github.pagehelper.PageHelper;

import cn.gov.msa.dao.AmanuensisMapper;
import cn.gov.msa.dao.AnchoragDefineMapper;
import cn.gov.msa.dao.ApplyMapper;
import cn.gov.msa.dao.ApplyMaterialAnnexMapper;
import cn.gov.msa.dao.ApplyMaterialMapper;
import cn.gov.msa.dao.AuditOpinionMapper;
import cn.gov.msa.dao.ControlDefineMapper;
import cn.gov.msa.dao.DeclarationsMapper;
import cn.gov.msa.dao.DeclareItemMaterialMapper;
import cn.gov.msa.dao.FlowBusinessMapper;
import cn.gov.msa.dao.FlowInstanceMapper;
import cn.gov.msa.dao.FlowNodeParamMapper;
import cn.gov.msa.dao.ForeignRescueMapper;
import cn.gov.msa.dao.ForeignWatersPermitMapper;
import cn.gov.msa.dao.NavigationaidsMapper;
import cn.gov.msa.dao.ProhibitedAreaDefineMapper;
import cn.gov.msa.dao.ProhibitedAreaPermitMapper;
import cn.gov.msa.dao.RecordCertificateFileMapper;
import cn.gov.msa.dao.RecordCertificateListMapper;
import cn.gov.msa.dao.RecordCompanyMapper;
import cn.gov.msa.dao.RecordPersonMapper;
import cn.gov.msa.dao.RecordSafetyWorkInlandMapper;
import cn.gov.msa.dao.RecordSafetyWorkPortMapper;
import cn.gov.msa.dao.RecordShipMapper;
import cn.gov.msa.dao.RouteDefineMapper;
import cn.gov.msa.dao.ScavengLiabiInsCerMapper;
import cn.gov.msa.dao.ShipMapper;
import cn.gov.msa.dao.ShipwrecksWorkMapper;
import cn.gov.msa.dao.TowingWorkMapper;
import cn.gov.msa.dao.VerifyMapper;
import cn.gov.msa.dao.WatersSafetyMapper;
import cn.gov.msa.dao.WatersWorkMapper;
import cn.gov.msa.dao.WatersWorkXYMapper;
import cn.gov.msa.dao.WorkDeviceMapper;
import cn.gov.msa.dao.WorkspaceDefineMapper;
import cn.gov.msa.dao.YachtClubRecordMapper;
import cn.gov.msa.model.Amanuensis;
import cn.gov.msa.model.AnchoragDefine;
import cn.gov.msa.model.Apply;
import cn.gov.msa.model.ApplyMaterial;
import cn.gov.msa.model.ApplyMaterialAnnex;
import cn.gov.msa.model.AuditOpinion;
import cn.gov.msa.model.ControlDefine;
import cn.gov.msa.model.Declarations;
import cn.gov.msa.model.DeclareItem;
import cn.gov.msa.model.DeclareItemMaterial;
import cn.gov.msa.model.FlowBusiness;
import cn.gov.msa.model.FlowNode;
import cn.gov.msa.model.FlowNodeParam;
import cn.gov.msa.model.ForeignRescue;
import cn.gov.msa.model.ForeignWatersPermit;
import cn.gov.msa.model.Navigationaids;
import cn.gov.msa.model.ProhibitedAreaDefine;
import cn.gov.msa.model.ProhibitedAreaPermit;
import cn.gov.msa.model.RecordCertificateFile;
import cn.gov.msa.model.RecordCertificateList;
import cn.gov.msa.model.RecordCompany;
import cn.gov.msa.model.RecordPerson;
import cn.gov.msa.model.RecordSafetyWorkInland;
import cn.gov.msa.model.RecordSafetyWorkPort;
import cn.gov.msa.model.RecordShip;
import cn.gov.msa.model.RouteDefine;
import cn.gov.msa.model.ScavengLiabiInsCer;
import cn.gov.msa.model.Ship;
import cn.gov.msa.model.ShipwrecksWork;
import cn.gov.msa.model.TowingWork;
import cn.gov.msa.model.VSelectMaterial;
import cn.gov.msa.model.Verify;
import cn.gov.msa.model.WatersSafety;
import cn.gov.msa.model.WatersWork;
import cn.gov.msa.model.WatersWorkXY;
import cn.gov.msa.model.WorkDevice;
import cn.gov.msa.model.WorkspaceDefine;
import cn.gov.msa.model.YachtClubRecord;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.ApplyService;
import cn.gov.msa.service.DeclareItemService;
import cn.gov.msa.service.FlowService;
import cn.gov.msa.util.ESSPDFClientToolUtil;
import cn.gov.msa.util.HtmlToPdf;
import cn.gov.msa.util.PropertiesTools;
import cn.org.bjca.seal.esspdf.client.tools.ESSPDFClientTool;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

/**
 * @Title: ApplyServiceImpl.java
 * @Package cn.gov.msa.service.impl
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 何宇江
 * @date 2018年3月16日 下午2:28:45
 */
public class ApplyServiceImpl implements ApplyService {
	@Autowired
	ApplyMapper applyMapper;
	@Autowired
	WatersSafetyMapper watersSafetyMapper;
	@Autowired
	WatersWorkMapper watersWorkMapper;
	@Autowired
	WatersWorkXYMapper watersWorkXYMapper;
	@Autowired
	WorkDeviceMapper workDeviceMapper;
	@Autowired
	ShipwrecksWorkMapper shipwrecksWorkMapper;
	@Autowired
	TowingWorkMapper towingWorkMapper;
	@Autowired
	RecordSafetyWorkInlandMapper recordSafetyWorkInlandMapper;
	@Autowired
	ProhibitedAreaDefineMapper prohibitedAreaDefineMapper;
	@Autowired
	RouteDefineMapper routeDefineMapper;
	@Autowired
	ControlDefineMapper controlDefineMapper;
	@Autowired
	AnchoragDefineMapper anchoragDefineMapper;
	@Autowired
	WorkspaceDefineMapper workspaceDefineMapper;
	@Autowired
	ScavengLiabiInsCerMapper scavengLiabiInsCerMapper;
	@Autowired
	RecordCompanyMapper recordCompanyMapper;
	@Autowired
	RecordCertificateFileMapper recordCertificateFileMapper;
	@Autowired
	RecordCertificateListMapper recordCertificateListMapper;
	@Autowired
	DeclarationsMapper declarationsMapper;
	@Autowired
	ProhibitedAreaPermitMapper prohibitedAreaPermitMapper;
	@Autowired
	RecordSafetyWorkPortMapper recordSafetyWorkPortMapper;
	@Autowired
	ApplyMaterialMapper applyMaterialMapper;
	@Autowired
	ApplyMaterialAnnexMapper applyMaterialAnnexMapper;
	@Autowired
	ForeignWatersPermitMapper foreignWatersPermitMapper;
	@Autowired
	YachtClubRecordMapper yachtClubRecordMapper;
	@Autowired
	NavigationaidsMapper navigationaidsMapper;
	@Autowired
	ForeignRescueMapper foreignRescueMapper;
	@Autowired
	FlowService flowService;
	@Autowired
	FlowInstanceMapper flowInstanceMapper;
	@Autowired
	FlowBusinessMapper flowBusinessMapper;
	@Autowired
	DeclareItemService declareItemService;

	@Autowired
	ShipMapper shipMapper;
	@Autowired
	RecordShipMapper recordShipMapper;
	@Autowired
	RecordPersonMapper recordPersonMapper;
	@Autowired
	VerifyMapper verifyMapper;

	private static String url = PropertiesTools.readProperties("project.url");
	private static String path = PropertiesTools.readProperties("project.path");

	@Transactional
	@Override
	public List<Apply> selectData(int pageNum, int pageSize, Apply apply) {
		String[] queryParam = new String[2];
		queryParam[0] = "cn.gov.msa.pagehelper.CustomOracleParse";
		queryParam[1] = " fnc_getdictnamebycode('DM_XKXMLX', itemtypecode) itemtypename";
		CustomPageHelper.startPage(pageNum, pageSize, queryParam);

//		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码
		return applyMapper.selectData(apply);
	}

	@Transactional
	@Override
	public List<Apply> selectDataHadDone(int pageNum, int pageSize, Apply apply) {
		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码
		return applyMapper.selectDataHadDone(apply);
	}

	@Override
	@Transactional
	public String saveData(JSONObject jsonApply, JSONObject jsonBusiness, JSONArray jaMaterial, JSONArray jaShip,
			JSONArray jaCoordinate, JSONArray jaVerify, JSONArray jaDevice,boolean issubmit) throws Exception {
		String applyno = "";
		String orgcode = "";
		String orgname = "";

		String nodecode = "";
		String nodename = "";

		String applyid = "";

		if (jsonApply.has("applyid"))
			applyid = jsonApply.getString("applyid");
		else
			applyid = UUID.randomUUID().toString().toUpperCase();

		// 申请单表数据保存
		Apply apply = (Apply) JSONObject.toBean(jsonApply, Apply.class);
		String itemname = apply.getItemname();
		// 获取对应的流程,并得到节点的FlowNode
		DeclareItem declareItem = declareItemService.getInfoByItemno(apply.getItemno());
		String approveflow = declareItem.getApproveflow();
		FlowBusiness flowBusiness = flowBusinessMapper.selectByPrimaryKey(approveflow);

		if (flowBusiness == null) {
			throw new NullPointerException();
		} else {
			String flowNumber = flowBusiness.getFlownumber();
			if (issubmit) {
				FlowNode flowNode = flowService.getAcceptNode(flowNumber);
				nodecode = flowNode.getNodecode();
				nodename = flowNode.getNodename();
			} else {
				FlowNode flowNode = flowService.getFistNode(flowNumber);
				nodecode = flowNode.getNodecode();
				nodename = flowNode.getNodename();
			}
			// 获取对应的流程,并得到节点的FlowNode

			apply.setApplyid(applyid);
			apply.setNodeno(nodecode);
			apply.setNodename(nodename);

			int i = 0;
			if (jsonApply.has("applyid")){
				Date submittime = new Date();
				apply.setSubmittime(submittime);
				
				i = applyMapper.updateByPrimaryKeySelective(apply);
			}
			else
				i = applyMapper.insert(apply);

			orgcode = apply.getOrgcode();
			orgname = apply.getOrgname();
			// 业务数据保存
			int j = 0;
			String itemno = apply.getItemno();

			try { // 解决JSONObject.toBean的日期转换时在bean里变成当前时间的问题，很重要
				String[] dateFormats = new String[] { "yyyy-MM-dd" };
				JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
			} catch (Exception e) {
				System.err.println("转换出错");
				e.printStackTrace();
			}

			switch (itemno) {
			case "1001": // 通航水域岸线安全使用许可
				WatersSafety watersSafety = (WatersSafety) JSONObject.toBean(jsonBusiness, WatersSafety.class);
				watersSafety.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = watersSafetyMapper.updateByPrimaryKeySelective(watersSafety);
				else
					j = watersSafetyMapper.insert(watersSafety);
				break;
			case "1002": // 通航水域水上水下活动作业许可
				WatersWork watersWork = (WatersWork) JSONObject.toBean(jsonBusiness, WatersWork.class);
				watersWork.setApplyid(applyid);
				if (jsonApply.has("applyid"))
					j = watersWorkMapper.updateByPrimaryKeySelective(watersWork);
				else
					j = watersWorkMapper.insert(watersWork);
				break;
			case "1003": // 通航水域水上水下活动作业许可（变更）
				WatersWork watersWork1 = (WatersWork) JSONObject.toBean(jsonBusiness, WatersWork.class);
				watersWork1.setApplyid(applyid);
				if (jsonApply.has("applyid"))
					j = watersWorkMapper.updateByPrimaryKeySelective(watersWork1);
				else
					j = watersWorkMapper.insert(watersWork1);
				break;
			case "1004": // 外国籍船舶或飞机从事海上搜救审批申请
				ForeignRescue foreignRescue = (ForeignRescue) JSONObject.toBean(jsonBusiness, ForeignRescue.class);
				foreignRescue.setApplyid(applyid);
				if (jsonApply.has("applyid"))
					j = foreignRescueMapper.updateByPrimaryKeySelective(foreignRescue);
				else
					j = foreignRescueMapper.insertSelective(foreignRescue);
				break;
			case "1005": // 打捞或者拆除沿海水域内沉船沉物审批
				ShipwrecksWork shipwrecksWork = (ShipwrecksWork) JSONObject.toBean(jsonBusiness, ShipwrecksWork.class);
				shipwrecksWork.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = shipwrecksWorkMapper.updateByPrimaryKeySelective(shipwrecksWork);
				else
					j = shipwrecksWorkMapper.insert(shipwrecksWork);
				break;
			case "1006": // 沿海大型设施和移动式平台水上拖带许可
				TowingWork towingWork = (TowingWork) JSONObject.toBean(jsonBusiness, TowingWork.class);
				towingWork.setApplyid(applyid);
				if (jsonApply.has("applyid"))
					j = towingWorkMapper.updateByPrimaryKeySelective(towingWork);
				else
					j = towingWorkMapper.insert(towingWork);
				break;
			case "1007": // 禁航区划定许可
				ProhibitedAreaDefine prohibitedAreaDefine = (ProhibitedAreaDefine) JSONObject.toBean(jsonBusiness,
						ProhibitedAreaDefine.class);
				prohibitedAreaDefine.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = prohibitedAreaDefineMapper.updateByPrimaryKeySelective(prohibitedAreaDefine);
				else
					j = prohibitedAreaDefineMapper.insert(prohibitedAreaDefine);
				break;
			case "1008": // 航路划定审批
				RouteDefine routeDefine = (RouteDefine) JSONObject.toBean(jsonBusiness, RouteDefine.class);
				routeDefine.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = routeDefineMapper.updateByPrimaryKeySelective(routeDefine);
				else
					j = routeDefineMapper.insert(routeDefine);
				break;
			case "1009": // 交通管制区划定许可
				ControlDefine controlDefine = (ControlDefine) JSONObject.toBean(jsonBusiness, ControlDefine.class);
				controlDefine.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = controlDefineMapper.updateByPrimaryKeySelective(controlDefine);
				else
					j = controlDefineMapper.insert(controlDefine);
				break;
			case "1010": // 港外锚地划定许可
				AnchoragDefine anchoragDefine = (AnchoragDefine) JSONObject.toBean(jsonBusiness, AnchoragDefine.class);
				anchoragDefine.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = anchoragDefineMapper.updateByPrimaryKeySelective(anchoragDefine);
				else
					j = anchoragDefineMapper.insert(anchoragDefine);
				break;
			case "1011": // 安全作业区划定许可
				WorkspaceDefine workspaceDefine = (WorkspaceDefine) JSONObject.toBean(jsonBusiness,
						WorkspaceDefine.class);
				workspaceDefine.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = workspaceDefineMapper.updateByPrimaryKeySelective(workspaceDefine);
				else
					j = workspaceDefineMapper.insert(workspaceDefine);
				break;
			case "1012": // 残骸清除责任保险或其他财务保证证书签发
				ScavengLiabiInsCer scavengLiabiInsCer = (ScavengLiabiInsCer) JSONObject.toBean(jsonBusiness,
						ScavengLiabiInsCer.class);
				scavengLiabiInsCer.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = scavengLiabiInsCerMapper.updateByPrimaryKeySelective(scavengLiabiInsCer);
				else
					j = scavengLiabiInsCerMapper.insert(scavengLiabiInsCer);
				break;
			case "1013": // 海事声明签注业务申请
				Declarations declarations = (Declarations) JSONObject.toBean(jsonBusiness, Declarations.class);
				declarations.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = declarationsMapper.updateByPrimaryKeySelective(declarations);
				else
					j = declarationsMapper.insert(declarations);
				break;
			case "1014": // 船舶进入或穿越禁航区许可
				ProhibitedAreaPermit prohibitedAreaPermit = (ProhibitedAreaPermit) JSONObject.toBean(jsonBusiness,
						ProhibitedAreaPermit.class);
				prohibitedAreaPermit.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = prohibitedAreaPermitMapper.updateByPrimaryKeySelective(prohibitedAreaPermit);
				else
					j = prohibitedAreaPermitMapper.insert(prohibitedAreaPermit);
				break;
			case "1015": // 内河通航水域安全作业备案
				RecordSafetyWorkInland recordSafetyWorkInland = (RecordSafetyWorkInland) JSONObject.toBean(jsonBusiness,
						RecordSafetyWorkInland.class);
				recordSafetyWorkInland.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = recordSafetyWorkInlandMapper.updateByPrimaryKeySelective(recordSafetyWorkInland);
				else
					j = recordSafetyWorkInlandMapper.insert(recordSafetyWorkInland);
				break;
			case "2001": // 外国籍船舶进入或临时进入非对外开放水域许可申请
				ForeignWatersPermit foreignWatersPermit = (ForeignWatersPermit) JSONObject.toBean(jsonBusiness,
						ForeignWatersPermit.class);
				foreignWatersPermit.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = foreignWatersPermitMapper.updateByPrimaryKeySelective(foreignWatersPermit);
				else
					j = foreignWatersPermitMapper.insertSelective(foreignWatersPermit);
				break;
			case "2002": // 船舶在港区水域内安全作业备案
				JSONArray jaWordType = jsonBusiness.getJSONArray("worktype");
				String worktype = "";
				for (Object object : jaWordType) {
					worktype += object.toString() + ",";
				}
				worktype = worktype.substring(0, worktype.length() - 1);
				jsonBusiness.put("worktype", worktype);

				RecordSafetyWorkPort recordSafetyWorkPort = (RecordSafetyWorkPort) JSONObject.toBean(jsonBusiness,
						RecordSafetyWorkPort.class);
				recordSafetyWorkPort.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = recordSafetyWorkPortMapper.updateByPrimaryKeySelective(recordSafetyWorkPort);
				else
					j = recordSafetyWorkPortMapper.insert(recordSafetyWorkPort);
				break;
			case "2003": // 游艇俱乐部备案申请
				YachtClubRecord yachtClubRecord = (YachtClubRecord) JSONObject.toBean(jsonBusiness,
						YachtClubRecord.class);
				yachtClubRecord.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = yachtClubRecordMapper.updateByPrimaryKeySelective(yachtClubRecord);
				else
					j = yachtClubRecordMapper.insertSelective(yachtClubRecord);
				break;
			case "9001": // 海区专用航标设置、移动、撤除审批
				Navigationaids navigationaids = (Navigationaids) JSONObject.toBean(jsonBusiness, Navigationaids.class);
				navigationaids.setApplyid(applyid);

				if (jsonApply.has("applyid"))
					j = navigationaidsMapper.updateByPrimaryKeySelective(navigationaids);
				else
					j = navigationaidsMapper.insertSelective(navigationaids);
				break;
			case "10001": // 单位资料备案
				jsonApply.put("applyid", applyid);
				this.saveCompanyRecord(jsonApply, jsonBusiness);
				break;
			case "10002": // 船舶资料备案
				jsonApply.put("applyid", applyid);
				this.saveShipRecord(jsonApply, jsonBusiness);
				break;
			case "10003": // 船舶资料备案
				jsonApply.put("applyid", applyid);
				this.savePersonRecord(jsonApply, jsonBusiness);
				break;
			}

			// 附报材料信息保存
			if (jsonApply.has("applyid")) {
				int idA = applyMaterialAnnexMapper.deleteByApplyid(applyid);
				int idM = applyMaterialMapper.deleteByApplyid(applyid);
			}
			if (jaMaterial != null) {
				for (int k = 0; k < jaMaterial.size(); k++) {
					JSONObject jsonMaterial = jaMaterial.getJSONObject(k);
					JSONArray jaMaterialAnnex = new JSONArray();
					if (jsonMaterial.has("MaterialAnnex")) {
						jaMaterialAnnex = jsonMaterial.getJSONArray("MaterialAnnex");
						jsonMaterial.remove("MaterialAnnex");
					}

					// 附报材料 [Bis_ApplyMaterial]
					String attachmaterialid = UUID.randomUUID().toString().toUpperCase();
					ApplyMaterial applyMaterial = (ApplyMaterial) JSONObject.toBean(jsonMaterial, ApplyMaterial.class);
					applyMaterial.setApplyid(applyid);
					applyMaterial.setAttachmaterialid(attachmaterialid);
					applyMaterialMapper.insert(applyMaterial);

					// 附报材料附件[Bis_ApplyMaterialAnnex]
					for (Object object2 : jaMaterialAnnex) {
						JSONObject jsonMaterialAnnex = JSONObject.fromObject(object2);
						ApplyMaterialAnnex applyMaterialAnnex = (ApplyMaterialAnnex) JSONObject
								.toBean(jsonMaterialAnnex, ApplyMaterialAnnex.class);
						applyMaterialAnnex.setAttachmaterialid(attachmaterialid);
						applyMaterialAnnexMapper.insert(applyMaterialAnnex);
					}
				}
			}

			// 保存船舶数据
			if (jaShip != null && jaShip.size() > 0) {
				// Ship ship = (Ship)JSONObject.toBean(jsonShip, Ship.class);
				// ship.setApplyid(applyid);
				//
				// int iShip = 0;
				// if(jsonApply.has("applyid"))
				// iShip = shipMapper.updateByPrimaryKeySelective(ship);
				// else
				// iShip = shipMapper.insert(ship);

				int iShip = 0;
				if (jsonApply.has("applyid"))
					iShip = shipMapper.deleteByApplyid(applyid);

				for (Object object : jaShip) {
					Ship ship = (Ship) JSONObject.toBean(JSONObject.fromObject(object), Ship.class);
					ship.setApplyid(applyid);

					iShip = shipMapper.insert(ship);
				}

//				if (jsonApply.has("applyid")) {
//					List shipidnumberList = new ArrayList();
//					for (Object object : jaShip) {
//						Ship ship = (Ship) JSONObject.toBean(JSONObject.fromObject(object), Ship.class);
//						shipidnumberList.add(ship.getShipidnumber());
//					}
//					if (jaVerify != null && jaVerify.size() > 0) {
//						for (Object object : jaVerify) {
//							Verify verify = (Verify) JSONObject.toBean(JSONObject.fromObject(object), Verify.class);
//							shipidnumberList.remove(verify.getShipidnumber());
//						}
//					}
//					verifyMapper.delete(jsonApply.getString("applyid"), shipidnumberList); // 删除时保留数据库现有的记录，只删除有变更的数据
//				}
//				// 有船舶数据一定有校验数据，保存校验数据
//				if (jaVerify != null && jaVerify.size() > 0) {
//					for (Object object : jaVerify) {
//						Verify verify = (Verify) JSONObject.toBean(JSONObject.fromObject(object), Verify.class);
//						String uuid = UUID.randomUUID().toString();
//						verify.setApplyid(applyid);
//						verify.setVerifyresultid(uuid);
//						verifyMapper.insert(verify);
//					}
//				}
			}

			// 保存坐标数据
			if (jaCoordinate.size() > 0) {
				int iWatersWorkXY = 0;
				if (jsonApply.has("applyid"))
					iWatersWorkXY = watersWorkXYMapper.deleteByApplyid(applyid);

				for (Object object : jaCoordinate) {
					WatersWorkXY watersWorkXY = (WatersWorkXY) JSONObject.toBean(JSONObject.fromObject(object),
							WatersWorkXY.class);
					watersWorkXY.setApplyid(applyid);

					iWatersWorkXY = watersWorkXYMapper.insert(watersWorkXY);
				}
			}
			
			// 保存设备及主要技术参数
			if (jaDevice.size() > 0) {
				if (jsonApply.has("applyid"))
					workDeviceMapper.deleteByApplyid(applyid);
				for (Object object : jaDevice) {
					WorkDevice workDevice = (WorkDevice) JSONObject.toBean(JSONObject.fromObject(object), WorkDevice.class);
					workDevice.setApplyid(applyid);
					workDeviceMapper.insertSelective(workDevice);
				}
			}

			applyno = apply.getApplyno();
			if (i > 0 && issubmit) {
				String senduserid = apply.getProposerid();
				String sendusername = apply.getProposer();

				// 启动流程
				String businessnumber = declareItem.getApproveflow();
				flowService.startFlow(flowBusiness.getFlownumber(), applyid, businessnumber, nodecode, orgcode,
						senduserid, sendusername, itemno, applyno, itemname, orgname);

				// if(!StringUtils.isEmpty(apply.getMobilephone())) {
				// try {
				// SmsTools.sendMsg(apply.getMobilephone(), "您的业务已经申报成功");
				// } catch (Exception e) {
				// e.printStackTrace();
				// }
				// }

			}
		}

		return applyno;
	}

	@Autowired
	DeclareItemMaterialMapper declareItemMaterialMapper;

	@Override
	public List<DeclareItemMaterial> getMaterial(String itemno) throws Exception {

		DeclareItemMaterial declareItemMaterial = new DeclareItemMaterial();
		declareItemMaterial.setItemno(itemno);
		List<DeclareItemMaterial> declareItemMaterialList = declareItemMaterialMapper.selectData(declareItemMaterial);

		return declareItemMaterialList;
	}

	@Override
	public String getInfo(String applyno) throws Exception {
		JSONObject jsonApply = new JSONObject();
		JSONObject jsonBusiness = new JSONObject();
		JSONArray jaMaterial = new JSONArray();
		JSONArray jaShip = new JSONArray();
		JSONArray jaCoordinate = new JSONArray();
		JSONArray jaDevice = new JSONArray();

		Apply apply = applyMapper.selectByApplyno(applyno);
		jsonApply = JSONObject.fromObject(apply);

		String itemno = apply.getItemno();
		String applyid = apply.getApplyid();

		switch (itemno) {
		case "1001":
			WatersSafety watersSafety = watersSafetyMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(watersSafety);
			break;
		case "1002":
			WatersWork watersWork = watersWorkMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(watersWork);

			List<WatersWorkXY> watersWorkXYList = watersWorkXYMapper.selectByApplyid(applyid);
			jaCoordinate = JSONArray.fromObject(watersWorkXYList);
			
			List<WorkDevice> workDeviceList = workDeviceMapper.selectByApplyid(applyid);
			jaDevice = JSONArray.fromObject(workDeviceList);
			break;
		case "1003":
			WatersWork watersWork1 = watersWorkMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(watersWork1);

			List<WatersWorkXY> watersWorkXYList1 = watersWorkXYMapper.selectByApplyid(applyid);
			jaCoordinate = JSONArray.fromObject(watersWorkXYList1);
			break;
		case "1004":
			ForeignRescue foreignRescue = foreignRescueMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(foreignRescue);
			break;
		case "1005":
			ShipwrecksWork shipwrecksWork = shipwrecksWorkMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(shipwrecksWork);
			
			List<WatersWorkXY> watersWorkXY = watersWorkXYMapper.selectByApplyid(applyid);
			jaCoordinate = JSONArray.fromObject(watersWorkXY);
			
			List<WorkDevice> workDevice = workDeviceMapper.selectByApplyid(applyid);
			jaDevice = JSONArray.fromObject(workDevice);
			break;
		case "1006":
			TowingWork towingWork = towingWorkMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(towingWork);
			break;
		case "1007":
			ProhibitedAreaDefine prohibitedAreaDefine = prohibitedAreaDefineMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(prohibitedAreaDefine);
			break;
		case "1008":
			RouteDefine routeDefine = routeDefineMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(routeDefine);
			break;
		case "1009":
			ControlDefine controlDefine = controlDefineMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(controlDefine);
			break;
		case "1010":
			AnchoragDefine anchoragDefine = anchoragDefineMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(anchoragDefine);
			break;
		case "1011":
			WorkspaceDefine workspaceDefine = workspaceDefineMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(workspaceDefine);
			break;
		case "1012":
			ScavengLiabiInsCer scavengLiabiInsCer = scavengLiabiInsCerMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(scavengLiabiInsCer);
			break;
		case "1013":
			Declarations declarations = declarationsMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(declarations);
			break;
		case "1014":
			ProhibitedAreaPermit prohibitedAreaPermit = prohibitedAreaPermitMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(prohibitedAreaPermit);
			break;
		case "1015":
			RecordSafetyWorkInland recordSafetyWorkInland = recordSafetyWorkInlandMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(recordSafetyWorkInland);
			break;
		case "2001":
			ForeignWatersPermit foreignWatersPermit = foreignWatersPermitMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(foreignWatersPermit);
			break;
		case "2002":
			RecordSafetyWorkPort recordSafetyWorkPort = recordSafetyWorkPortMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(recordSafetyWorkPort);
			break;
		case "2003":
			YachtClubRecord yachtClubRecord = yachtClubRecordMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(yachtClubRecord);
			break;
		case "9001":
			Navigationaids navigationaids = navigationaidsMapper.selectByPrimaryKey(applyid);
			jsonBusiness = JSONObject.fromObject(navigationaids);
			break;
		case "10001":
			JSONObject recordCompany = JSONObject.fromObject(recordCompanyMapper.selectByApplyid(applyid));
			if (recordCompany != null && !recordCompany.isNullObject()) {
				JSONArray recordcertificatelist = recordCertificateListMapper
						.selectByRecordid(recordCompany.getString("recordid"));
				for (int i = 0; i < recordcertificatelist.size(); i++) {
					if (recordcertificatelist.getJSONObject(i) != null
							&& !recordcertificatelist.getJSONObject(i).isNullObject()) {
						JSONArray recordcertificatefile = recordCertificateFileMapper.selectByCertificateid(
								recordcertificatelist.getJSONObject(i).getString("certificateid"));
						recordcertificatelist.getJSONObject(i).put("recordcertificatefile", recordcertificatefile);
					}
				}
				recordCompany.put("recordcertificatelist", recordcertificatelist);
			}
			jsonBusiness = JSONObject.fromObject(recordCompany);
			break;
		case "10002":
			JSONObject recordShip = JSONObject.fromObject(recordShipMapper.selectByApplyId(applyid));
			if (recordShip != null && !recordShip.isNullObject()) {
				JSONArray recordcertificatelist = recordCertificateListMapper
						.selectByRecordid(recordShip.getString("recordid"));
				for (int i = 0; i < recordcertificatelist.size(); i++) {
					if (recordcertificatelist.getJSONObject(i) != null
							&& !recordcertificatelist.getJSONObject(i).isNullObject()) {
						JSONArray recordcertificatefile = recordCertificateFileMapper.selectByCertificateid(
								recordcertificatelist.getJSONObject(i).getString("certificateid"));
						recordcertificatelist.getJSONObject(i).put("recordcertificatefile", recordcertificatefile);
					}
				}
				recordShip.put("recordcertificatelist", recordcertificatelist);
			}
			jsonBusiness = JSONObject.fromObject(recordShip);
			break;
		case "10003":
			JSONObject recordPerson = JSONObject.fromObject(recordPersonMapper.selectByApplyId(applyid));
			if (recordPerson != null && !recordPerson.isNullObject()) {
				JSONArray recordcertificatelist = recordCertificateListMapper
						.selectByRecordid(recordPerson.getString("recordid"));
				for (int i = 0; i < recordcertificatelist.size(); i++) {
					if (recordcertificatelist.getJSONObject(i) != null
							&& !recordcertificatelist.getJSONObject(i).isNullObject()) {
						JSONArray recordcertificatefile = recordCertificateFileMapper.selectByCertificateid(
								recordcertificatelist.getJSONObject(i).getString("certificateid"));
						recordcertificatelist.getJSONObject(i).put("recordcertificatefile", recordcertificatefile);
					}
				}
				recordPerson.put("recordcertificatelist", recordcertificatelist);
			}
			jsonBusiness = JSONObject.fromObject(recordPerson);
			break;
		}

		jaShip = JSONArray.fromObject(shipMapper.selectByApplyid(applyid));

		VSelectMaterial vSelectMaterial = new VSelectMaterial();
		vSelectMaterial.setApplyid(applyid);
		List<VSelectMaterial> vSelectMaterialList = applyMaterialMapper.selectMaterialByApplyid(vSelectMaterial);
		jaMaterial = JSONArray.fromObject(vSelectMaterialList);

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("apply", jsonApply);
		jsonResult.put("business", jsonBusiness);
		jsonResult.put("material", jaMaterial);
		jsonResult.put("coordinate", jaCoordinate);
		if (jaDevice.size() > 0)
			jsonResult.put("device", jaDevice);
		if (jaShip.size() > 0)
			jsonResult.put("ship", jaShip);

		return jsonResult.toString();
	}

	@Autowired
	AuditOpinionMapper auditOpinionMapper;

	@Override
	public String getCorrectionInfo(String applyno) throws Exception {
		JSONArray jaMaterial = new JSONArray();
		JSONObject jsonApply = new JSONObject();

		Apply apply = applyMapper.selectByApplyno(applyno);
		jsonApply = JSONObject.fromObject(apply);

		VSelectMaterial vSelectMaterial = new VSelectMaterial();
		vSelectMaterial.setApplyid(apply.getApplyid());
		vSelectMaterial.setIscorrection(1);
		List<VSelectMaterial> vSelectMaterialList = applyMaterialMapper.selectMaterialData(vSelectMaterial);
		jaMaterial = JSONArray.fromObject(vSelectMaterialList);

		AuditOpinion auditOpinion = auditOpinionMapper.selectLastData(apply.getApplyid());

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("apply", jsonApply);
		jsonResult.put("material", jaMaterial);
		jsonResult.put("opintion", auditOpinion);

		return jsonResult.toString();
	}

	@Override
	public String getAcceptInfo(String applyid) throws Exception {

		String nodecode = "";
		String nodename = "";

		Apply apply = applyMapper.selectByPrimaryKey(applyid);

		// 获取对应的流程,并得到节点的FlowNode
		DeclareItem declareItem = declareItemService.getInfoByItemno(apply.getItemno());
		String approveflow = declareItem.getApproveflow();
		FlowBusiness flowBusiness = flowBusinessMapper.selectByPrimaryKey(approveflow);

		String flowNumber = flowBusiness.getFlownumber();

		FlowNode flowNode = flowService.getAcceptNode(flowNumber);
		nodecode = flowNode.getNodecode();
		nodename = flowNode.getNodename();

		AuditOpinion record = new AuditOpinion();
		record.setBusinessid(applyid);
		record.setNodecode(nodecode);

		AuditOpinion auditOpinion = auditOpinionMapper.selectLastData(applyid);

		JSONObject jsonResult = JSONObject.fromObject(auditOpinion);

		return jsonResult.toString();
	}

	@Autowired
	AmanuensisMapper amanuensisMapper;
	@Autowired
	FlowNodeParamMapper flowNodeParamMapper;

	@Override
	public boolean saveAmanuensis(String applyno, String rulenum) throws Exception {
		boolean save = false;

		try {
			// 获取节点所需的文书
			List<FlowNodeParam> flowNodeParamList = flowNodeParamMapper.selectByApplyno(applyno);
			if (flowNodeParamList.size() > 0) {
				FlowNodeParam flowNodeParam = flowNodeParamList.get(0);

				String amanuensiscodes = flowNodeParam.getBookcode();
				String amanuensisnames = flowNodeParam.getBookname();
				System.err.println("该节点应产生的文书" + amanuensiscodes);
				if (!StringUtils.isEmpty(amanuensiscodes)) {
					System.err.println(amanuensiscodes.split(","));
					System.err.println(amanuensiscodes.split(",").length);
					if (amanuensiscodes != null && !"".equals(amanuensiscodes)) {
						for (int i = 0; i < amanuensiscodes.split(",").length; i++) { // 一个节点可能会生成几个文书.
							Date now = new Date();
							SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmssSSS");
							String filename = ft.format(now) + ".pdf";
							String srcPath = url + "/amanuensis/" + amanuensiscodes.split(",")[i] + ".html?applyno="
									+ applyno;
							String destPath = path + "/gahCMP/amanuensis/file/" + applyno + "/" + filename;

							Apply apply = applyMapper.selectByApplyno(applyno);
							Amanuensis amanuensis = new Amanuensis();
							
							boolean b = HtmlToPdf.convert(srcPath, destPath);
							if (b) {
								try{
									if(null != rulenum){
										String newpath = path + "/gahCMP/amanuensis/file/" + applyno + "/sign_" + filename;
										boolean pdf = ESSPDFClientToolUtil.pdfSign(rulenum, destPath, newpath);
										if(pdf){
											destPath = newpath;
										}
										else{
											System.err.println("印章生成失败");
										}
									}
								}
								catch(Exception e){
									System.err.println("印章生成异常" + e.getMessage());
								}
								
								File file = new File(destPath);
								amanuensis.setAttachment(FileCopyUtils.copyToByteArray(file));
							}

							amanuensis.setApplyid(apply.getApplyid());
							amanuensis.setNodecode(apply.getNodeno());
							amanuensis.setNodename(apply.getNodename());
							amanuensis.setAmanuensiscode(amanuensiscodes.split(",")[i]);
							amanuensis.setAmanuensisname(amanuensisnames.split(",")[i]);
							amanuensis.setFilename(filename);
							amanuensis.setFileurl("/gahCMP/amanuensis/file/" + applyno + "/" + filename);

							int k = 0;
							List<Amanuensis> amanuensisList = amanuensisMapper.selectData(amanuensis);

							if (amanuensisList.size() == 0 || apply.getNodename().indexOf("补正") >= 0) {
								k = amanuensisMapper.insert(amanuensis);
							} else {
								amanuensis.setAmanuensisid(amanuensisList.get(0).getAmanuensisid());
								k = amanuensisMapper.updateByPrimaryKeySelective(amanuensis);
							}

							save = k > 0 ? true : false;
						}
					}
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			save = false;
		}

		return save;
	}
	
	/*
	 * 供任务调度（ScheduleServiceImpl）使用，其它地方不建议使用
	 * */
	@Override
	public void saveAmanuensisByTask() throws Exception{
		List<Amanuensis> amanuensisList = amanuensisMapper.selectNoAttachment();
		
		for (Amanuensis amanuensis : amanuensisList) {
			try {
				String applyid = amanuensis.getApplyid();
				Apply apply = applyMapper.selectByPrimaryKey(applyid);

				String applyno = apply.getApplyno();
				
				
				// 获取节点所需的文书
					String amanuensiscodes = amanuensis.getAmanuensiscode();
					String amanuensisnames = amanuensis.getAmanuensisname();
					System.err.println("该节点应产生的文书" + amanuensiscodes);
					if (!StringUtils.isEmpty(amanuensiscodes)) {
						System.err.println(amanuensiscodes.split(","));
						System.err.println(amanuensiscodes.split(",").length);
						if (amanuensiscodes != null && !"".equals(amanuensiscodes)) {
							for (int i = 0; i < amanuensiscodes.split(",").length; i++) { // 一个节点可能会生成几个文书.
								Date now = new Date();
								SimpleDateFormat ft = new SimpleDateFormat("yyyyMMddhhmmssSSS");
								String filename = ft.format(now) + ".pdf";
								String srcPath = url + "/amanuensis/" + amanuensiscodes.split(",")[i] + ".html?applyno="
										+ applyno;
								String destPath = path + "/gahCMP/amanuensis/file/" + applyno + "/" + filename;

								
								boolean b = HtmlToPdf.convert(srcPath, destPath);
								if (b) {
									File file = new File(destPath);
									amanuensis.setAttachment(FileCopyUtils.copyToByteArray(file));
								}

								amanuensis.setApplyid(apply.getApplyid());
								amanuensis.setNodecode(apply.getNodeno());
								amanuensis.setNodename(apply.getNodename());
								amanuensis.setAmanuensiscode(amanuensiscodes.split(",")[i]);
								amanuensis.setAmanuensisname(amanuensisnames.split(",")[i]);
								amanuensis.setFilename(filename);
								amanuensis.setFileurl("/gahCMP/amanuensis/file/" + applyno + "/" + filename);

								int k = 0;
								List<Amanuensis> amanuensisList1 = amanuensisMapper.selectData(amanuensis);

								if (amanuensisList1.size() == 0 || apply.getNodename().indexOf("补正") >= 0) {
									k = amanuensisMapper.insert(amanuensis);
								} else {
									amanuensis.setAmanuensisid(amanuensisList1.get(0).getAmanuensisid());
									k = amanuensisMapper.updateByPrimaryKeySelective(amanuensis);
								}
							}
						}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@Override
	public List<Amanuensis> getAmanuensis(Amanuensis amanuensis) throws Exception {

		List<Amanuensis> amanuensisList = amanuensisMapper.selectData(amanuensis);

		return amanuensisList;
	}

	@Override
	public Amanuensis getAmanuensisById(String amanuensisid) throws Exception {
		Amanuensis amanuensis = amanuensisMapper.selectByPrimaryKey(amanuensisid);
		return amanuensis;
	}

	@Override
	public int deelteApply(String applyid) throws Exception {
		int iDelete = 0;

		Apply apply = new Apply();
		apply.setApplyid(applyid);
		apply.setIsdelete(1);

		iDelete = applyMapper.updateByPrimaryKeySelective(apply);

		return iDelete;
	}

	@Override
	@Transactional
	public void saveCompanyRecord(JSONObject jsonApply, JSONObject jsonBusiness) throws Exception {
		jsonBusiness.put("orgcode", jsonApply.getString("orgcode"));
		jsonBusiness.put("orgname", jsonApply.getString("orgname"));
		jsonBusiness.put("companyid", jsonApply.getString("companyid"));
		jsonBusiness.put("userid", jsonApply.getString("proposerid"));
		jsonBusiness.put("applydate", new Date());
		jsonBusiness.put("applyid", jsonApply.getString("applyid"));
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { // 只要设置这个数组，指定过滤哪些字段。
				"recordcertificate" });
		config.setRootClass(RecordCompany.class);
		RecordCompany recordCompany = (RecordCompany) JSONObject.toBean(JSONObject.fromObject(jsonBusiness, config),
				RecordCompany.class);
		if (jsonBusiness.has("recordid")) {
			recordCompanyMapper.updateByPrimaryKeySelective(recordCompany);
			recordCertificateListMapper.deleteByRecordid(jsonBusiness.getString("recordid"));
		} else {
			recordCompany.setRecordid(UUID.randomUUID().toString());
			recordCompanyMapper.insert(recordCompany);
		}
		JSONArray recordcertificate = jsonBusiness.optJSONArray("recordcertificate");
		for (int i = 0; i < recordcertificate.size(); i++) {
			RecordCertificateList recordCertificateList = (RecordCertificateList) JSONObject
					.toBean(recordcertificate.getJSONObject(i), RecordCertificateList.class);
			recordCertificateList.setRecordid(recordCompany.getRecordid());
			recordCertificateListMapper.insert(recordCertificateList);
			List<String> recordCertificateFileIdList = Arrays
					.asList(recordcertificate.getJSONObject(i).getString("fileid").split(","));
			for (int j = 0; j < recordCertificateFileIdList.size(); j++) {
				RecordCertificateFile recordCertificateFile = new RecordCertificateFile();
				recordCertificateFile.setFileid(recordCertificateFileIdList.get(j));
				recordCertificateFile.setCertificateid(recordCertificateList.getCertificateid());
				recordCertificateFileMapper.insert(recordCertificateFile);
			}
		}
	}

	@Override
	@Transactional
	public void saveShipRecord(JSONObject jsonApply, JSONObject jsonBusiness) throws Exception {
		jsonBusiness.put("orgcode", jsonApply.getString("orgcode"));
		jsonBusiness.put("userid", jsonApply.getString("proposerid"));
		jsonBusiness.put("applydate", new Date());
		jsonBusiness.put("applyid", jsonApply.getString("applyid"));
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { // 只要设置这个数组，指定过滤哪些字段。
				"recordcertificate" });
		config.setRootClass(RecordShip.class);
		RecordShip recordShip = (RecordShip) JSONObject.toBean(JSONObject.fromObject(jsonBusiness, config),
				RecordShip.class);
		if (jsonBusiness.has("recordid")) {
			recordShipMapper.updateByPrimaryKeySelective(recordShip);
			recordCertificateListMapper.deleteByRecordid(jsonBusiness.getString("recordid"));
		} else {
			String uuid = UUID.randomUUID().toString();
			recordShip.setRecordid(uuid);
			recordShipMapper.insertSelective(recordShip);
		}
		JSONArray recordcertificate = jsonBusiness.optJSONArray("recordcertificate");
		for (int i = 0; i < recordcertificate.size(); i++) {
			RecordCertificateList recordCertificateList = (RecordCertificateList) JSONObject
					.toBean(recordcertificate.getJSONObject(i), RecordCertificateList.class);
			recordCertificateList.setRecordid(recordShip.getRecordid());
			recordCertificateListMapper.insert(recordCertificateList);
			List<String> recordCertificateFileIdList = Arrays
					.asList(recordcertificate.getJSONObject(i).getString("fileid").split(","));
			for (int j = 0; j < recordCertificateFileIdList.size(); j++) {
				RecordCertificateFile recordCertificateFile = new RecordCertificateFile();
				recordCertificateFile.setFileid(recordCertificateFileIdList.get(j));
				recordCertificateFile.setCertificateid(recordCertificateList.getCertificateid());
				recordCertificateFileMapper.insert(recordCertificateFile);
			}
		}
	}

	@Override
	@Transactional
	public void savePersonRecord(JSONObject jsonApply, JSONObject jsonBusiness) throws Exception {
		jsonBusiness.put("orgcode", jsonApply.getString("orgcode"));
		jsonBusiness.put("userid", jsonApply.getString("proposerid"));
		jsonBusiness.put("applydate", new Date());
		jsonBusiness.put("applyid", jsonApply.getString("applyid"));
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[] { // 只要设置这个数组，指定过滤哪些字段。
				"recordcertificate" });
		config.setRootClass(RecordPerson.class);
		RecordPerson recordPerson = (RecordPerson) JSONObject.toBean(JSONObject.fromObject(jsonBusiness, config),
				RecordPerson.class);
		if (jsonBusiness.has("recordid")) {
			recordPersonMapper.updateByPrimaryKeySelective(recordPerson);
			recordCertificateListMapper.deleteByRecordid(jsonBusiness.getString("recordid"));
		} else {
			String uuid = UUID.randomUUID().toString();
			recordPerson.setRecordid(uuid);
			recordPersonMapper.insertSelective(recordPerson);
		}
		JSONArray recordcertificate = jsonBusiness.optJSONArray("recordcertificate");
		for (int i = 0; i < recordcertificate.size(); i++) {
			RecordCertificateList recordCertificateList = (RecordCertificateList) JSONObject
					.toBean(recordcertificate.getJSONObject(i), RecordCertificateList.class);
			recordCertificateList.setRecordid(recordPerson.getRecordid());
			recordCertificateListMapper.insert(recordCertificateList);
			List<String> recordCertificateFileIdList = Arrays
					.asList(recordcertificate.getJSONObject(i).getString("fileid").split(","));
			for (int j = 0; j < recordCertificateFileIdList.size(); j++) {
				RecordCertificateFile recordCertificateFile = new RecordCertificateFile();
				recordCertificateFile.setFileid(recordCertificateFileIdList.get(j));
				recordCertificateFile.setCertificateid(recordCertificateList.getCertificateid());
				recordCertificateFileMapper.insert(recordCertificateFile);
			}
		}
	}

	public int saveComment(Apply apply) throws Exception {
		int i = 0;

		i = applyMapper.updateByPrimaryKeySelective(apply);

		return i;
	}
	

	public List<RecordCompany> selectCompany(RecordCompany recordCompany) throws Exception{
		
		return recordCompanyMapper.selectData(recordCompany);
		
	}
}
